package com.huibei.wateradmin.authority.service;

import com.huibei.wateradmin.authority.domain.Menu;
import com.huibei.wateradmin.authority.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by wanglinlin on 2016/8/2.
 */
@Service
@Transactional
public class AccountService {
    @Autowired
    private UserService userService;
    @Autowired
    private MenuService menuService;

    public Boolean checkAuthority(User userInfo, String servletPath) {
        User user = userService.findById(userInfo.getUserId());
        Menu menuByUrl = menuService.findByURL(servletPath);
        if (menuByUrl == null) {
            return true;
        }

        List<Menu> menus = user.getRole().getMenus();
        Boolean hasAuth = true;

        List<Menu> list = menus.stream().filter(menu -> menu.getUrl().equals(servletPath)).collect(Collectors.toList());

        if (list.size() > 0) {
            hasAuth = true;
        }
        if (user.getUserId() == 2) {
            hasAuth = true;//超级管理员 暂时可以访问全部权限
        }

        return hasAuth;
    }
}
